/** Request */
package domain.session;

import java.text.ParseException;
import java.util.logging.Logger;

import org.json.JSONObject;

import domain.action.Action;

/**
 * Request which will give a response
 * @author jessienewman
 * @version $Revision: 1.0 $
 */
public class Request {
	
	/**
	 * Field action.
	 */
	private final Action action;
	
	/**
	 * Field request.
	 */
	private JSONObject requestJSON; // $codepro.audit.disable variableShouldBeFinal
	
	/**
	 * Field json.
	 */
	private JSONObject json;
	
	/**
	 * Field LOGGER.
	 */
	private static final Logger LOGGER = Logger.getLogger(Request.class.getName());
	
	/**
	 * Constructor for Request.
	 */
	public Request() {
		action = null;
		requestJSON = new JSONObject();
	}
	
	/**
	 * Constructor for Request.
	 * @param requestStr String
	 */
	public Request(final String requestStr) {
		try {
			json = new JSONObject(requestStr);
			requestJSON = new JSONObject(requestStr);
		} catch (final ParseException ex) {
			LOGGER.warning("Parse Exception" + ex);
		}
		
		action = Action.getAction(requestJSON.getString("action"));
		requestJSON.remove("action");
	}
	
	/**
	 * Constructor for Request.
	 * @param action Action
	 * @param request JSONObject
	 */
	public Request(final Action action, final JSONObject request) {
		this.action = action;
		this.requestJSON = request;
		try {
			json = new JSONObject(request.toString());
			json.put("action", action);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			LOGGER.info("parse exception" + e);
			e.printStackTrace();
		}
		
	}
	
	/**
	 * Method getJSON.
	
	 * @return JSONObject */
	public JSONObject getJSON() {
		return json;
	}
	
	/**
	 * Method getRequest.
	
	 * @return JSONObject */
	public JSONObject getRequest() {
		return requestJSON;
	}
	
	/**
	 * Method toString.
	
	 * @return String */
	public String toString() {
		return json.toString();
	}
	
	/**
	 * Method getAction.
	
	 * @return Action */
	public Action getAction() {
		return action;
	}
}
